package com.nisure.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.nisure.common.CommonData;
import com.nisure.common.GetHttpJsonobject;
import com.nisure.common.R;
import com.nisure.entity.CompanyBackupEntity;
import com.nisure.service.CompanyBackupService;

@RestController
public class LoginController {
	@Autowired
	private GetHttpJsonobject getHttpJsonobject;
	@Autowired
	private CompanyBackupService companyBackupService;

	@RequestMapping("/sendIdentify")
	public R sendIdentify(HttpServletRequest request) {
		String phoneNumber = request.getParameter("jsPhone");
		String url = "http://localhost:20171/simi/yuanshengSimi.do?sendBaiduAppSms";
		JSONObject params2 = new JSONObject();
		params2.put("mobile_num", phoneNumber);
		JSONObject totalJson = new JSONObject();
		JSONObject response = getHttpJsonobject.httpPOST(url, "mobile_num=" + phoneNumber + "&type=4&appname='原力提词'");
		// {"msg":"send success","success":true,"jsonStr":"{\"msg\":\"send
		// success\",\"success\":true,\"attributes\":{\"smscode\":\"322626\"}}","attributes":{"smscode":"322626"}}
		response = JSONObject.parseObject(response.getString("attributes"));
		
		long identify = Long.valueOf(response.getString("smscode"));

		if (identify > 0) {
			HttpSession session = request.getSession();
			session.setAttribute("identify", identify);
			session.setMaxInactiveInterval(1 * 60);// 一分钟过期
			return R.ok().put("result", "success");
		}
		return R.ok().put("result", "error");
	}

	@RequestMapping("/checkIdentify")
	public R checkIdentify(HttpServletRequest request) {
		HttpSession session = request.getSession();

		long jsIdentify = Long.valueOf(request.getParameter("jsVerification"));
		long identify = Long.valueOf(session.getAttribute("identify").toString());
		if (jsIdentify == identify) {
			return R.ok().put("result", "success");
		}
		return R.ok().put("result", "error");
	}

	@RequestMapping("/showIndex")
	public ModelAndView showIndex(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("index.html");
		long userId = Long.valueOf(request.getSession().getAttribute("userId").toString());

		String phone = request.getParameter("jsPhone");
		if (userId != 0 && phone != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("appId", CommonData.appId);
			String userName = request.getSession().getAttribute("userName").toString();
			Date visitTime = new Date();
			map.put("userName", userName);
			map.put("createTime", visitTime);
			map.put("visitTime", visitTime);
			map.put("phoneNumber", phone);
			// 执行插入
			int result = companyBackupService.addBackup(map);
			if (result > 0) {
				return mav;
			}

		}
		return null;
	}

}
